<template>
  <div class="container">
    <input type="checkbox" :value="item.isDone" @change="handleCheck" />
    <span class="text" :class="{ 'deleted-text': isCheck }">{{ item.name }}</span>
    <i class="iconfont icon-delete" @click="deleteTask"></i>
  </div>
</template>

<script>
export default {
  name: 'todoItem',
  data() {
    return {
      isCheck: this.item.isDone
    }
  },
  props: {
    item: {
      type: Object,
      required: true
    }
  },
  methods: {
    handleCheck(e) {
      this.isCheck = e.target.checked
      this.$emit('toggle', this.item.id, e.target.checked)
    },
    deleteTask() {
      const confirmed = window.confirm('确定删除该任务吗?')
      if (confirmed) {
        this.$emit('delete', this.item.id)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ccc;
  font-size: 20px;
  input {
    margin-right: 10px;
  }
  .text {
    margin-right: auto;
  }
  .deleted-text {
    text-decoration: line-through;
  }
  .icon-delete {
    color: red;
    font-size: 22px;
  }
}
</style>
